/**
 * 
 */
package uk.ac.glasgow.demosocs.users;

import java.io.Serializable;


/**
 * @author 1105877c
 *
 */
public class Voter extends User implements Serializable{

	private boolean mVoted;
	//whether the voter has voted for the election
	
	private boolean mNominated;
	
	//election role in string
	private String mRole;
	
	//voted people - voted target
	private CandidateImpl mVotedTarget;
	
	public static final String VOTER = "Voter";
	public static final String CANDIDATE = "Candidate";
	public static final String ADMIN = "Admin";
	public static final String NOMINEE = "Nominee";

	
	/**
	 * @param surname
	 * @param forename
	 * @param GUID
	 * @param password
	 */
	public Voter(String surname, String forename, String GUID, String password) {
		super(surname, forename, GUID, password);
		mVoted = false;
		setRole(VOTER);
		
		mVotedTarget = null;
	}

	/**
	 * the voter cast a vote.
	 * target is the candidate to be voted
	 * The voter will be checked whether voted before, if yes the previous one will be covered.
	 * Candidate's votes count will plus 1
	 */
	public void castVote(CandidateImpl target){
		if(isVoted()){
			//if the voter voted before
			resetVote();
			castVote(target);
		} else{
			//if the voter havent voted before
			target.beVotedByOthers();
			//voted people got incremented
			
			mVotedTarget = target;
			//set the voted target
			mVoted = true;
		}
	}
	
	/**
	 * voter reset the vote
	 */
	public void resetVote(){
		if(!mVotedTarget.equals(null)){
			//get the vote back
			mVotedTarget.decrementVotes();
		}
		mVoted = false;
		mVotedTarget = null;
	}
	
	/**
	 * does the voter voted, true for voted
	 * @return true for voted, false for not
	 */
	public boolean isVoted(){
		return mVoted && !mVotedTarget.equals(null);
	}
	
	/**
	 * cast nomination
	 * @return true - successfully nominate, false - have nominated before
	 */
	public boolean castNominate(){
		if(mNominated)
			return false;
		else{
			mNominated = true;
			return true;
		}
	}
	
	public void restoreNominate(){
		mNominated = false;
	}
	
	/**
	 * does the voter nominated, true for already nominated
	 * @return boolean
	 */
	public boolean isNominated(){
		return mNominated;
	}
	
	/**
	 * change election role
	 */
	public void beNominatedByOther(){
		setRole(NOMINEE);
	}
	
	public void setRole(String role){
		mRole = role;
	}
	
	public String getRole(){
		return mRole;
	}
	
}
